package com.qianfeng.lingshijia;

import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.GridView;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.google.gson.Gson;
import com.handmark.pulltorefresh.library.ILoadingLayout;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;

import adapter.ImportListAdapter;
import entity.HeadEntity;
import entity.TotalList_Import_Entity;
import utils.DownloadUtils;

public class TotalListActivity_Import extends AppCompatActivity implements View.OnClickListener, RadioGroup.OnCheckedChangeListener {

    RadioGroup radioGroup;
    RadioButton radioButton_price;
    boolean priceIsChecked = true;

    PullToRefreshListView totallist_importList;
    String URL = "http://cmsproxy.haolingzui.com/importBulkProducts.do";
    int pageNumber = -1;
    String sortOrder = "0";
    String sortType = "1";
    ImportListAdapter importListAdapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_total_list_activity__import);
        Intent intent = getIntent();
        if (intent!=null)
        {
            URL = intent.getStringExtra("url");
        }
        initView();
        loadData();
    }

    /**
     * 初始化控件
     */
    private void initView()
    {
        totallist_importList = (PullToRefreshListView) findViewById(R.id.totallist_import_List);
        importListAdapter = new ImportListAdapter(getApplicationContext());
        totallist_importList.setAdapter(importListAdapter);
        //设置刷新
        refresh(totallist_importList);

        radioGroup = (RadioGroup) findViewById(R.id.rg_totallist);
        radioButton_price = (RadioButton) findViewById(R.id.rb_totallist_price);
        radioButton_price.setOnClickListener(this);
        radioGroup.setOnCheckedChangeListener(this);

        //设置默认点击
        RadioButton rb_totallist_all = (RadioButton) findViewById(R.id.rb_totallist_all);
        rb_totallist_all.setChecked(true);

    }

    /**
     * 刷新设置
     * @param totallist_importList
     */
    private void refresh(PullToRefreshListView totallist_importList)
    {
        totallist_importList.setMode(PullToRefreshBase.Mode.BOTH);
        ILoadingLayout startLabels = totallist_importList.getLoadingLayoutProxy(true,false);
        startLabels.setPullLabel("下拉刷新...");// 刚下拉时，显示的提示
        startLabels.setRefreshingLabel("正在载入...");// 刷新时
        startLabels.setReleaseLabel("放开刷新...");// 下来达到一定距离时，显示的提示

        // 设置上拉刷新文本
        ILoadingLayout endLabels = totallist_importList.getLoadingLayoutProxy(false, true);
        endLabels.setPullLabel("上拉刷新...");// 刚下拉时，显示的提示
        endLabels.setRefreshingLabel("正在载入...");// 刷新时
        endLabels.setReleaseLabel("放开刷新...");// 下来达到一定距离时，显示的提示

        totallist_importList.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>()
        {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView)
            {
                //下拉
                pageNumber = -1;
                importListAdapter.clearData();
                loadData();
            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView)
            {
                //上拉
                loadData();
            }
        });
    }

    /**
     * 数据加载
     */
    private void loadData()
    {
        pageNumber ++;
        //热销单品数据加载
        RequestParams params = new RequestParams();
        //page=0&pageSize=9&sortOrder=0&sortType=1
        params.addBodyParameter("page", pageNumber + "");
        params.addBodyParameter("pageSize", "20");
        params.addBodyParameter("sortOrder", sortOrder);
        params.addBodyParameter("sortType", sortType);

        DownloadUtils.postJson(URL, new RequestCallBack<String>() {
            @Override
            public void onSuccess(ResponseInfo<String> responseInfo) {
                if (responseInfo != null) {
                    String response = responseInfo.result;
                    Gson gson = new Gson();
                    TotalList_Import_Entity entity = gson.fromJson(response, TotalList_Import_Entity.class);
                    //importListAdapter.setData(entity.getData().getPList());
                    importListAdapter.addData(entity.getData().getPList());
                }
                totallist_importList.onRefreshComplete();
            }

            @Override
            public void onFailure(HttpException error, String msg) {
                error.printStackTrace();
                totallist_importList.onRefreshComplete();
            }
        }, params);
    }



    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId)
    {
        switch (checkedId)
        {
            case R.id.rb_totallist_all:
                importListAdapter.clearData();
                setPriceUpDown();
                //page=0&pageSize=20&sortOrder=0&sortType=1 综合
                pageNumber = -1;
                sortOrder= "0";
                sortType = "1";
                loadData();
                break;
            case R.id.rb_totallist_hotsale:
                importListAdapter.clearData();
                setPriceUpDown();
                //page=0&pageSize=20&sortOrder=0&sortType=2  热销
                pageNumber = -1;
                sortOrder= "0";
                sortType = "2";
                loadData();
                break;
            case R.id.rb_totallist_price:
                break;
        }
    }

    public void setPriceUpDown()
    {
        priceIsChecked = true;
        Drawable drawable_updown= getResources().getDrawable(R.drawable.updown_icon);
        drawable_updown.setBounds(0, 0, drawable_updown.getIntrinsicWidth(), drawable_updown.getIntrinsicHeight());
        radioButton_price.setCompoundDrawables(null,null,drawable_updown,null);
    }

    /**
     * 点击价格时，箭头的上下变动
     * @param v
     */
    @Override
    public void onClick(View v)
    {
        Drawable drawable_up= getResources().getDrawable(R.drawable.price_up);
        Drawable drawable_down= getResources().getDrawable(R.drawable.price_down);
        if (priceIsChecked)
        {
            drawable_up.setBounds(0, 0, drawable_up.getIntrinsicWidth(), drawable_up.getIntrinsicHeight());
            radioButton_price.setCompoundDrawables(null,null,drawable_up,null);
            priceIsChecked = false;
            //page=0&pageSize=20&sortOrder=1&sortType=3 价格低到高
            pageNumber = -1;
            sortOrder= "1";
            sortType = "3";
            importListAdapter.clearData();
            loadData();

        }else{
            drawable_down.setBounds(0,0,drawable_down.getIntrinsicWidth(),drawable_down.getIntrinsicHeight());
            radioButton_price.setCompoundDrawables(null,null,drawable_down,null);
            priceIsChecked = true;
            //page=0&pageSize=20&sortOrder=0&sortType=3 高到底
            pageNumber = -1;
            sortOrder= "0";
            sortType = "3";
            importListAdapter.clearData();
            loadData();
        }
    }

    /**
     * 点击回退返回
     * @param view
     */
    public void back(View view)
    {
        this.finish();
    }
}
